package ch.zbw.pra.sysventory.ui.gui.panels.overviewandsearchpanel.subpanels;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ch.zbw.pra.sysventory.domain.factories.FileGenerator;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator.FileType;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;

public class SearchPanel extends JPanel implements KeyListener {

	private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SearchPanel.class);

	private static final long serialVersionUID = 5965424881728985849L;

	private final String searchText = "Bitte Suchbegriff eingeben";

	private JTextField searchComputerField;
	private JTextField searchIpField;
	private JTextField searchCompTypField;
	private JTextField searchProzField;
	private JTextField searchOsField;
	private JLabel imageLabel;
	private ImageIcon lupeIcon;
	private JButton startScanButton;

	public SearchPanel() {
		this.lupeIcon = new ImageIcon(new FileGenerator().getFile(FileType.ICON_LOUPE).getAbsolutePath());
		this.imageLabel = new JLabel(this.lupeIcon);
		this.searchComputerField = new JTextField(this.searchText);
		this.searchComputerField.addKeyListener(this);
		this.searchIpField = new JTextField(this.searchText);
		this.searchIpField.addKeyListener(this);
		this.searchCompTypField = new JTextField(this.searchText);
		this.searchCompTypField.addKeyListener(this);
		this.searchProzField = new JTextField(this.searchText);
		this.searchProzField.addKeyListener(this);
		this.searchOsField = new JTextField(this.searchText);
		this.searchOsField.addKeyListener(this);

		// searchField.addKeyListener(this);
		this.searchComputerField.addFocusListener(new FocusAdapter() {

			@Override
			public void focusGained(FocusEvent evt) {
				SearchPanel.this.searchComputerField.selectAll();
			}
		});
		this.searchComputerField.addKeyListener(this);
		this.startScanButton = new JButton("Search");
		init();
	}

	public void init() {
		// imageLabel.add(lupeIcon);
		// this.add(lupeIcon);
		this.setBackground(Color.white);
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(this);
		double icon = 0.0;
		double label = 0.2;
		double textField = 1.0;
		gridBagLayout.columnWeights = new double[] { icon, label, textField, label, textField, label, textField };

		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();
		Insets leftInset = new Insets(5, 7, 0, 0);
		Insets endLine = new Insets(5, 7, 5, 5);
		gridBagConstraints.insets = new Insets(5, 5, 5, 0);
		int x = 0;
		int y = 0;

		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 2, this.imageLabel);
		gridBagConstraints.insets = leftInset;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel("Computer"));
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, this.searchComputerField);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel("IP"));
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, this.searchIpField);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel("Computer-Typ"));
		gridBagConstraints.insets = endLine;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, this.searchCompTypField);
		gridBagConstraints.insets = leftInset;
		x = 1;
		y++;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel("Processor"));
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, this.searchProzField);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel("Betriebsystem"));
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, this.searchOsField);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());
		gridBagConstraints.insets = endLine;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, this.startScanButton);
		this.setVisible(true);
	}

	public String getTextComputerField() {
		String computer = this.searchComputerField.getText();
		return computer.contains(this.searchText) ? "" : computer;
	}

	public String getTextSearchIpField() {
		String ip = this.searchIpField.getText();
		return ip.contains(this.searchText) ? "" : ip;
	}

	public String getTextSearchCompTyp() {
		String typ = this.searchCompTypField.getText();
		return typ.contains(this.searchText) ? "" : typ;
	}

	public String getTextProzField() {
		String proz = this.searchProzField.getText();
		return proz.contains(this.searchText) ? "" : proz;
	}

	public String getTextOsField() {
		String os = this.searchOsField.getText();
		return os.contains(this.searchText) ? "" : os;
	}

	public void addActionStartSearchButtonListener(ActionListener listener) {
		if (listener != null) {
			this.startScanButton.addActionListener(listener);
		}

	}

	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
			this.startScanButton.doClick();
		}
	}

	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}
}
